﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm18 : System.Web.UI.Page
    {
        attend at = new attend();
        Classes cl = new Classes();
        subject sub = new subject();
        student stud = new student();
        protected void Page_Load(object sender, EventArgs e)
        {
            tbdate.Text = DateTime.Now.ToString();
            btnupdate.Visible = false;
            tbreason.Visible = false;
            RadioButtonList2.Visible = false;
            labreason.Visible = false;
            lableave.Visible = false;
            if(!IsPostBack)
            {
                DataTable dt1 = cl.GetRecords(-99, "", -99, false, " and ");
                ddclass.DataSource = dt1;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();
                //DataTable d2 = sub.GetRecords(-99, "", -99, -99, false, " and ");
                //ddsubject.DataSource = d2;
                //ddsubject.DataTextField = "subject_name";
                //ddsubject.DataValueField = "subject_id";
                //ddsubject.DataBind();

                DataTable d3 = stud.GetRecords(-99, "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
                ddstud.DataSource = d3;
                ddstud.DataTextField = "student_name";
                ddstud.DataValueField = "student_id";
                ddstud.DataBind();

                GridView1.Visible = false;
                GridView2.Visible = false;
                //DataTable dt = at.GetRecords(-99, "", -99, -99, -99, "", "", false, false, " and ");
                //GridView1.DataSource = dt;
                //GridView1.DataBind();
            }
        }          

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            labreason.Visible = true;
            lableave.Visible = true;
            DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "",-99, -99,"", "", false, false, " and ");
            tbdayid.Text = dt.Rows[0]["day_id"].ToString();
            tbdate.Text = dt.Rows[0]["date"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
            ddstud.SelectedValue = dt.Rows[0]["student_id"].ToString();
           // ddsubject.SelectedValue = dt.Rows[0]["subject_id"].ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["attendence"].ToString();
            if (RadioButtonList1.SelectedValue == "a")
            {
                tbreason.Visible = true;
                RadioButtonList2.Visible = true;
            }
            tbreason.Text = dt.Rows[0]["reason"].ToString();
            RadioButtonList2.SelectedValue = dt.Rows[0]["leave"].ToString();
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", false, false, " and ");
                at.day_id = Convert.ToInt64(dt.Rows[0]["day_id"].ToString());
                at.date = dt.Rows[0]["date"].ToString();
                at.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                at.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                //at.subject_id = Convert.ToInt64(dt.Rows[0]["subject_id"].ToString());
                at.attendence = dt.Rows[0]["attendence"].ToString();
                at.reason = dt.Rows[0]["reason"].ToString();
                at.leave =Convert.ToBoolean( dt.Rows[0]["leave"].ToString());
                at.isdeleted = true;
                at.SaveRecords();
                Response.Redirect("~/admin/Attendence.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            at.day_id = -99;
            at.date = tbdate.Text;
            at.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
            at.student_id = Convert.ToInt64(ddstud.SelectedValue.ToString());
            //at.subject_id = Convert.ToInt64(ddsubject.SelectedValue.ToString());
            at.attendence = RadioButtonList1.SelectedValue.ToString();
            if (at.attendence.Equals("p"))
            {
                tbreason.Visible = false;
                RadioButtonList2.Visible = false;
                tbreason.ReadOnly = true;
                at.reason = tbreason.Text;
                at.leave = false;
            }
            else
            {
                at.reason = tbreason.Text;
                at.leave = Convert.ToBoolean(RadioButtonList2.SelectedValue.ToString());
            }
            at.isdeleted = false;
            at.SaveRecords();
           // Label1.Text = "Data inserted.";

        }

        protected void ddclass_SelectedIndexChanged(object sender, EventArgs e)
        {

            
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            if (RadioButtonList1.SelectedValue=="a")
            {
                tbreason.Visible = true;
                RadioButtonList2.Visible = true;
                labreason.Visible = true;
                lableave.Visible = true;
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            at.day_id = Convert.ToInt64(tbdayid.Text);
            at.date = tbdate.Text;
            at.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
            at.student_id = Convert.ToInt64(ddstud.SelectedValue.ToString());
            //at.subject_id = Convert.ToInt64(ddsubject.SelectedValue.ToString());
            at.attendence = RadioButtonList1.SelectedValue.ToString();
            at.reason = tbreason.Text;
            at.leave = Convert.ToBoolean(RadioButtonList2.SelectedValue.ToString());
            at.isdeleted = false;
            at.SaveRecords();
            Response.Redirect("~/admin/Attendence.aspx");
            //Label1.Text = "Data updated.";

        }

        protected void btndelete_Command1(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", false, false, " and ");
                at.day_id = Convert.ToInt64(dt.Rows[0]["day_id"].ToString());
                at.date = dt.Rows[0]["date"].ToString();
                at.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                at.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                at.attendence = dt.Rows[0]["attendence"].ToString();
                at.reason = dt.Rows[0]["reason"].ToString();
                at.leave = Convert.ToBoolean(dt.Rows[0]["leave"].ToString());
                at.isdeleted = true;
                at.SaveRecords();
                Response.Redirect("~/admin/Attendence.aspx");
            }

        }

        protected void btnedit_Command1(object sender, CommandEventArgs e)
        {
            btninsert.Visible = false;
            btnupdate.Visible = true;

            DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", false, false, " and ");
            tbdayid.Text = dt.Rows[0]["day_id"].ToString();
            tbdate.Text = dt.Rows[0]["date"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
            ddstud.SelectedValue = dt.Rows[0]["student_id"].ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["attendence"].ToString();
            tbreason.Text = dt.Rows[0]["reason"].ToString();
            RadioButtonList2.SelectedValue = dt.Rows[0]["leave"].ToString();

        }

        protected void btnpresnt_Click(object sender, EventArgs e)
        {

            DataTable dt = at.GetRecords(-99, "", -99, -99,"p", "", false, false, " and ");
            GridView2.DataSource = dt;
            GridView2.DataBind();
            GridView2.Visible = true;
            GridView1.Visible = false;
            GridView3.Visible = false;

        }

        protected void btnabsent_Click(object sender, EventArgs e)
        {
            DataTable dt = at.GetRecords(-99, "", -99, -99,"a", "", false, false, " and ");
            GridView1.DataSource = dt;
            GridView1.DataBind();
            DataTable dt1 = at.GetRecords(-99, "", -99, -99, "a", "", true, false, " and ");
            GridView3.DataSource = dt1;
            GridView3.DataBind();
            GridView1.Visible = true;
            GridView2.Visible = false;
            GridView3.Visible = true;
        }

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btnedit_Command2(object sender, CommandEventArgs e)
        {
            btnupdate.Visible = true;
            btninsert.Visible = false;
            labreason.Visible = true;
            lableave.Visible = true;
            DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "a", "", true,false, " and ");
            tbdayid.Text = dt.Rows[0]["day_id"].ToString();
            tbdate.Text = dt.Rows[0]["date"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
            ddstud.SelectedValue = dt.Rows[0]["student_id"].ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["attendence"].ToString();
            if (RadioButtonList1.SelectedValue == "a")
            {
                RadioButtonList2.Visible = true;
                tbreason.Visible = true;
            }
            tbreason.Text = dt.Rows[0]["reason"].ToString();
            RadioButtonList2.SelectedValue = dt.Rows[0]["leave"].ToString();

        }

        protected void btndelete_Command2(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = at.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "a", "", true, false, " and ");
                at.day_id = Convert.ToInt64(dt.Rows[0]["day_id"].ToString());
                at.date = dt.Rows[0]["date"].ToString();
                at.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                at.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                at.attendence = dt.Rows[0]["attendence"].ToString();
                at.reason = dt.Rows[0]["reason"].ToString();
                at.leave = Convert.ToBoolean(dt.Rows[0]["leave"].ToString());
                at.isdeleted = true;
                at.SaveRecords();
                Response.Redirect("~/admin/Attendence.aspx");

            }
        }

        protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }      
        
    }
}
